//
//  HWshiyongjiluViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/11/3.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWshiyongjiluViewController.h"

#import "HWquanObjiect.h"
#import "HWMyVouchersquanTableViewCell.h"



@interface HWshiyongjiluViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) NSMutableArray *gameArr;
@property(strong,nonatomic) NSString *max_p;
@property(strong,nonatomic) NSString *p;

@end

@implementation HWshiyongjiluViewController

-(UITableView *)mainaTableVie {
    
    if (_mainaTableVie == nil
        ) {
        
        
        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
        _mainaTableVie.backgroundColor = RGB(247, 247, 247);
        _mainaTableVie.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"" titleStr:@"" detailStr:@"暂无数据"];
        _mainaTableVie.ly_emptyView.contentViewOffset = -40;
        
    }
    return   _mainaTableVie ;
}








- (void)viewDidLoad {

    [super viewDidLoad];
    // yishiyongde   baisebeijingjilu

    [self setupUI];
    
    
    
    
//    [self navigaidaohang];
    
    
    
}


- (void)navigaidaohang  {
    
    NSString *shiyong = [HWjianzhiduiObject keylocalization:@"使用记录"];
    UIBarButtonItem *but =   [[UIBarButtonItem alloc]initWithTitle:shiyong style:UIBarButtonItemStylePlain target:self action:@selector(shiyongjilu)];
    but.tintColor = RGB(68, 68, 68);
    
    self.navigationItem.rightBarButtonItem = but;
    
    
    
    
    
}

- (void)shiyongjilu {
    
    
    
}









- (void)setupUI {
    
    
  //   self.gameArr = [[NSMutableArray alloc]init];
    
    self.navigationItem.title  = @"代金券";
    self.navigationItem.title = [HWjianzhiduiObject keylocalization:@"代金券"];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mainaTableVie];
    [self addrefreshControl];
    
    [self.mainaTableVie mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(kScreenW);
        make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
    
}





- (void)addrefreshControl {
    
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
    [self.mainaTableVie.mj_header beginRefreshing];
    
     self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}

#pragma  mark --------- 下拉刷新
- (void)underRefresh {
    
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        // 结束刷新
    //        [self.mainaTableVie.mj_header endRefreshing];
    //    });
    
    
    // [SVProgressHUD show];
    
   
    
    self.p = @"1";

       __weak typeof(self) weakSelf = self;
    
      self.gameArr = [[NSMutableArray alloc]init];
      
         NSString *userID = [HWLocallyStoredData getLoginUserID];
         NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
         NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
         NSString *signMD5 = [WMUtil md5HexDigest:sign];
        NSString *typeStr =  @"1";
        NSDictionary *parametes = @{
                                        @"ct" :  @"app",
                                        @"ac" : @"mycoupon",
                                        @"ios" : @"1",
                                        @"uid": userID,
                                        @"t" :tStr,
                                        @"sign": signMD5,
                                        @"p" : @"1",
                                        @"type":typeStr,
                                        @"member_id":[HWLocallyStoredData getCode],
                        
                            
                                        };
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        // [SVProgressHUD dismiss];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

        if ([codeStr intValue] == 1) {
            
    
            
            self.gameArr =   [HWquanObjiect mj_objectArrayWithKeyValuesArray:response[@"data"]];
            
        
            self.p = response[@"p"];
            self.max_p = response[@"max_p"];
            [self.mainaTableVie reloadData];
            
            
            
            
           
        }
        
        
        
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
    }];
    
    
    
}




- (void)onRefresh {
    
    
    if ([self.p  intValue] > [self.max_p intValue]) {
        
        [MBProgressHUD  showError:@"请稍后加载数据"];
        
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        return;
    }
    
    
    
     
    
    
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
       NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
       NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
       NSString *signMD5 = [WMUtil md5HexDigest:sign];
      NSString *typeStr =  @"3";
     NSDictionary *parametes = @{
                                           @"ct" :  @"app",
                                           @"ac" : @"mycoupon",
                                           @"ios" : @"1",
                                           @"uid": userID,
                                           @"t" :tStr,
                                           @"sign": signMD5,
                                           @"p" : self.p,
                                           @"type":typeStr,
                                           @"member_id":[HWLocallyStoredData getCode],
                                           };
    
    
    
    
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        // [SVProgressHUD dismiss];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

        if ([codeStr intValue] == 1) {
            
         //    NSArray *arr1 =  response[@"data"][@"top"];

            NSArray *arr2 =  response[@"data"];
            
            
            
          
             NSMutableArray *mutableArr2 =    [HWquanObjiect mj_objectArrayWithKeyValuesArray:arr2];




            for (int  i = 0; i <  mutableArr2 .count ; i++) {

                [self.gameArr addObject:mutableArr2[i]];


            }

            
            
            
            self.p = response[@"p"];
            self.max_p = response[@"max_p"];
        
            [self.mainaTableVie reloadData];
            
            
            
            
           
        }
        
        
        
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
    }];
    
    
    
    
    
    
    
    
    
    
}






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  {
    
    
    
  //   return 10;
    return  self.gameArr.count;
    
    
}

 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    

    
    HWMyVouchersquanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell =  [[NSBundle mainBundle]loadNibNamed:@"HWMyVouchersquanTableViewCell" owner:nil options:nil].firstObject;
        
    }
    
    cell.shujumodel = self.gameArr[indexPath.row];
    

    return  cell;
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    return  94;
    
    
}


@end
